var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();
Page({
  data: {
    store: {
      id:0,
      address: '',
      storeName: '',
      range: '',
      tel: ''
    },
    storeId: 0
  },
  bindinputTel(event) {
    let store = this.data.store;
    store.tel = event.detail.value;
    this.setData({
      store: store
    });
  },
  bindinputName(event) {
    let store = this.data.store;
    store.storeName = event.detail.value;
    this.setData({
      store: store
    });
  },
  bindinputRang(event) {
    let store = this.data.store;
    store.range = event.detail.value;
    this.setData({
      store: store
    });
  },
  bindinputAdddress(event) {
    let store = this.data.store;
    store.address = event.detail.value;
    this.setData({
      store: store
    });
  },
  getStoreDetail() {
    let that = this;
    util.request(api.StoreDetail+"?token="+app.globalData.token, { id: that.data.storeId }).then(function (res) {
      if (res.code === "0000") {
        if(res.result){
            that.setData({
                store: res.result
            });
        }
      } else if(res.code==="0008"){
        wx.navigateTo({
          url: '/pages/auth/btnAuth/btnAuth',
        })
      }
    });
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (!wx.getStorageSync('userInfo') || !wx.getStorageSync('token')) {
      wx.navigateTo({
        url: '/pages/auth/btnAuth/btnAuth',
      })
    }else
      if (options.id) {
        this.setData({
          storeId: options.id
        });
        this.getStoreDetail();
      }
  },
  onReady: function () {

  },
  cancelStore(){
    wx.navigateBack({
      url: '/pages/ucenter/store/store',
    })
  },
  saveStore(){
    let store = this.data.store;

    if (store.storeName == '') {
      util.showErrorToast('请输入昵称');
      return false;
    }

    if (store.range == 0) {
      util.showErrorToast('请输入技能');
      return false;
    }

    if (store.address == '') {
      util.showErrorToast('请输入详细地址');
      return false;
    }

    let that = this;
    util.request(api.StoreSave+"?token="+app.globalData.token, { 
      id: store.id,
      storeName: store.storeName,
      tel: store.tel,
      range: store.range,
      address: store.address,
    }, 'POST', 'application/x-www-form-urlencoded').then(function (res) {
      if (res.code === "0000") {
        wx.navigateBack({
          url: '/pages/ucenter/store/store',
        })
      }
    });

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})